/*
 *	Copyright (C) 2008 - Mauricio Bieze Stefani
 *	This file is part of the Insano Game Engine.
 *
 *	Insano is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Insano is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Insano.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ClientConfig.h"
#include <iostream>
#include <sstream>
using namespace std;
#include <tinyxml.h>
#include <tinystr.h>

int ClientConfig::load(const char *filename)
{
	TiXmlDocument doc(filename);
	if (!doc.LoadFile())
		return -1;

	TiXmlHandle hDoc(&doc);

	TiXmlElement *vid=hDoc.FirstChild( "Video" ).Element();
	if (vid)
	{
		if(vid->Attribute("driver"))
		{
			if(strcmp("D3D9C",vid->Attribute("driver"))==0)
				video.driver=irr::video::EDT_DIRECT3D9;
			else if(strcmp("OPENGL",vid->Attribute("driver"))==0)
				video.driver=irr::video::EDT_OPENGL;
			else if(strcmp("SOFTWARE",vid->Attribute("driver"))==0)
				video.driver=irr::video::EDT_SOFTWARE;
			else if(strcmp("BURNINGS",vid->Attribute("driver"))==0)
				video.driver=irr::video::EDT_BURNINGSVIDEO;
			else if(strcmp("NULL",vid->Attribute("driver"))==0)
				video.driver=irr::video::EDT_NULL;
		}
		vid->QueryIntAttribute("hres",&video.horizontal);
		vid->QueryIntAttribute("vres",&video.vertical);
		int fs;
		vid->QueryIntAttribute("fullscreen",&fs);
		video.fullscreen=(fs!=0);
		vid->QueryIntAttribute("bpp", &video.bpp);
	}

	TiXmlElement *srv=hDoc.FirstChild( "Server" ).Element();
	if (srv)
	{
		int temp;
		if(srv->Attribute("hostname"))
			strcpy(host,srv->Attribute("hostname"));
		srv->QueryIntAttribute("port",&temp);
		port=temp;
		if(srv->Attribute("username"))
			strcpy(username,srv->Attribute("username"));
	}
	return 0;
}

int ClientConfig::save(const char *filename)
{
	TiXmlDocument doc;

 	TiXmlDeclaration *decl=new TiXmlDeclaration("1.0","","");
	doc.LinkEndChild(decl);

	TiXmlElement *vid=new TiXmlElement("Video");
	{
		if(video.driver==irr::video::EDT_DIRECT3D9)
			vid->SetAttribute("driver","D3D9C");
		else if(video.driver==irr::video::EDT_OPENGL)
			vid->SetAttribute("driver","OPENGL");
		else if(video.driver==irr::video::EDT_SOFTWARE)
			vid->SetAttribute("driver","SOFTWARE");
		else if(video.driver==irr::video::EDT_BURNINGSVIDEO)
			vid->SetAttribute("driver","BURNINGS");
		else if(video.driver==irr::video::EDT_NULL)
			vid->SetAttribute("driver","NULL");
		vid->SetAttribute("hres",video.horizontal);
		vid->SetAttribute("vres",video.vertical);
		vid->SetAttribute("fullscreen",(video.fullscreen?1:0));
		vid->SetAttribute("bpp",video.bpp);
	}
	doc.LinkEndChild(vid);

	TiXmlElement *srv=new TiXmlElement("Server");
		srv->SetAttribute("hostname",host);
		srv->SetAttribute("port",port);
		srv->SetAttribute("username",username);
	doc.LinkEndChild(srv);

	doc.SaveFile(filename);
	return 0;
}
